package user;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import Encryption.Encryptor;
import Hash.Hashing;
import db.PreparedQuery;

public class Reset {
	private String userName;
	private long tStamp;
	private String rKey;
	
	public static final String TABLE_NAME = "Reset";
	public static final String USER_NAME = TABLE_NAME + ".Name";
	public static final String TIME_STAMP = TABLE_NAME + ".timeStamp";
	public static final String RESET_KEY = TABLE_NAME + ".resetKey";


	
	public void resetLink() throws UnsupportedEncodingException, NoSuchAlgorithmException, SQLException {
		long lDateTime = new Date().getTime();
		String timeStamp = lDateTime +"";
		byte[] timeStampb = timeStamp.getBytes("UTF-8");
		String resetKey  = Encryptor.b2h(Hashing.getHash(this.getUserName(), timeStampb));
		String sql = "Insert into reset set " + USER_NAME + " =?, "  
				+ TIME_STAMP +" =?, "+ RESET_KEY + "=? " + "ON DUPLICATE KEY UPDATE resetKey =?" +",timeStamp=?;";
		PreparedQuery q = new PreparedQuery();
		try {
			
			q.open(sql);
			tStamp = lDateTime;
			rKey = resetKey;
			
			q.setParameter(1, this.getUserName());
			q.setParameter(2, lDateTime);
			q.setParameter(3, resetKey);
			q.setParameter(4, resetKey);
			q.setParameter(5, lDateTime);
			q.executeUpdate();
			// System.out.println(rs.toString());
			
		}catch (Exception e){
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		finally {
			q.close();
		}
		
	}
	
	public boolean checkTimeStamp() throws UnsupportedEncodingException, NoSuchAlgorithmException, SQLException, ClassNotFoundException {
		String sql = "SELECT " + TIME_STAMP + " FROM RESET WHERE resetKey =? ;";
		PreparedQuery q = new PreparedQuery();
		ResultSet rs = null;
		try {
			q.open(sql);
			q.setParameter(1, this.rKey);

			rs = q.executeQuery();
			if (rs.next()) {
				byte i = 1;
				this.tStamp = Long.parseLong(rs.getString(i++));
				long lDateTime = new Date().getTime();
				if(lDateTime-tStamp < 300000){
				return true;
				}else{
					return false;
				}
			}
			return false;
		} finally {
			if (rs != null)
				rs.close();
			q.close();
		}
	}
	
	public boolean deleteKey() throws SQLException, ClassNotFoundException{
		String userName = this.getName();
		if(userName!=null){
		 this.userName = userName;
		String sql = "Delete FROM RESET WHERE resetKey =? ;";
		PreparedQuery q = new PreparedQuery();
		ResultSet rs = null;
		try {
			q.open(sql);
			q.setParameter(1, this.rKey);

			q.executeUpdate();
			return true;
		}finally {
			if (rs != null)
				rs.close();
			q.close();
		}
		}else{
			return false;
		}
	}
	
	public String getName() throws SQLException, ClassNotFoundException{
		String sql = "SELECT " + USER_NAME + " FROM RESET WHERE resetKey =? ;";
		PreparedQuery q = new PreparedQuery();
		ResultSet rs = null;
		try {
			q.open(sql);
			q.setParameter(1, this.rKey);
			rs = q.executeQuery();

			if(rs.next()){
				byte i = 1;
				this.userName = rs.getString(i++);
				return this.userName;
			}else{
				return null;
			}
		}finally {
			if (rs != null)
				rs.close();
			q.close();
		}
	}
	
	public static boolean checkKey(String HashedKey) throws SQLException, ClassNotFoundException{
		String sql = "SELECT " + TIME_STAMP + " FROM RESET WHERE resetKey =? ;";
		PreparedQuery q = new PreparedQuery();
		ResultSet rs = null;
		try {
			q.open(sql);
			q.setParameter(1, HashedKey);

			rs = q.executeQuery();
			if (rs.next()) {
				return true;
			}else{
				return false;
			}
		}
		finally {
			if (rs != null)
				rs.close();
			q.close();
		}
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public long getTimeStamp() {
		return tStamp;
	}
	public void setTimeStamp(long timeStamp) {
		this.tStamp = timeStamp;
	}
	public String getrKey() {
		return rKey;
	}
	public void setrKey(String rKey) {
		this.rKey = rKey;
	}
}
